package com.xinhua.erp.service;

import com.xinhua.erp.pojo.PageResult;
import com.xinhua.erp.pojo.Pay;
import com.xinhua.erp.pojo.Result;
import com.xinhua.erp.pojo.Resale;
import com.xinhua.erp.repo.PayRepo;
import com.xinhua.erp.repo.ResaleRepo;
import com.xinhua.erp.utils.IdWorker;
import com.xinhua.erp.utils.OrderNumber;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Map;

@Service
@Transactional
public class ResaleService {
    @Autowired
    private ResaleRepo resaleRepo;

    @Autowired
    private PayRepo payRepo;

    @Autowired
    private IdWorker idWorker;

//    public Result updateState(Long id){

//        resaleRepo.updateState(id);

//        return new Result(true,20000,"审核成功");

//    }

    public Result findById(long id){

        return Result.OK(resaleRepo.getOne(id));
    }
    public Result findAll(String type,int page,int size){

        Pageable pageable = PageRequest.of(page - 1, size);

        Page<Resale> retails = resaleRepo.findAllByType(type,pageable);

        return Result.OK(new PageResult<>(retails.getTotalElements(),retails.getContent()));
    }
    public Result deleteById(long id){

        resaleRepo.deleteById(id);

        return Result.OK();
    }

    public Result save(Resale resale){

        resale.setId(idWorker.nextId());

        resale.setTime(LocalDateTime.now());

        //设置默认值
        resale.setOrderId(OrderNumber.getLs());

        resaleRepo.save(resale);

        Pay pay = new Pay();

        pay.setId(idWorker.nextId());

        pay.setAccountId(new Long(4));

        pay.setAccountName("南通建行");

        pay.setSupplier("个人");

        pay.setSupplierId(1214956071572066304L);

        pay.setUsername(resale.getOperator());

        pay.setAmount(resale.getAllPrice());

        if (resale.getType().equals("零售出库")){
            pay.setType("收款");
        }

        pay.setSerialno(OrderNumber.getSkNumber());

        pay.setTime(LocalDateTime.now());


        payRepo.save(pay);

        return Result.OK();
    }

    public Result update(Long id,Resale resale){
        resale.setId(id);
        resaleRepo.save(resale);
        return Result.OK();
    }
    public Result Search(Map searchMap, int page, int size) {

        Pageable pageable = PageRequest.of(page - 1, size);

        Page<Resale>retails = resaleRepo.findAll(createSpecification(searchMap), pageable);

        return Result.OK(new PageResult<>(retails.getTotalElements(), retails.getContent()));
    }

    private Specification<Resale> createSpecification(Map searchMap){
        return new Specification<Resale>() {
            @Override
            public Predicate toPredicate(Root<Resale> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                ArrayList<Predicate> predicateList = new ArrayList<>();
                if(!StringUtils.isBlank((String)searchMap.get("type"))){
                    predicateList.add(criteriaBuilder.like(root.get("type").as(String.class),"%"+searchMap.get("type")+"%"));
                }

                if(!StringUtils.isBlank((String)searchMap.get("orderId"))){
                    predicateList.add(criteriaBuilder.like(root.get("orderId").as(String.class),"%"+searchMap.get("orderId")+"%"));
                }
                if(!StringUtils.isBlank((String)searchMap.get("productName"))){
                    predicateList.add(criteriaBuilder.like(root.get("productName").as(String.class),"%"+searchMap.get("productName")+"%"));
                }
                if(!StringUtils.isBlank((String)searchMap.get("time"))){
                    predicateList.add(criteriaBuilder.like(root.get("time").as(String.class),"%"+searchMap.get("time")+"%"));
                }

                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }
}

